{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.2 \u7b80\u5355\u7684C\u6269\u5c55\u6a21\u5757\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u4e0d\u4f9d\u9760\u5176\u4ed6\u5de5\u5177\uff0c\u76f4\u63a5\u4f7f\u7528Python\u7684\u6269\u5c55API\u6765\u7f16\u5199\u4e00\u4e9b\u7b80\u5355\u7684C\u6269\u5c55\u6a21\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u7b80\u5355\u7684C\u4ee3\u7801\uff0c\u6784\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6269\u5c55\u6a21\u5757\u662f\u5f88\u5bb9\u6613\u7684\u3002\n\u4f5c\u4e3a\u7b2c\u4e00\u6b65\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4f60\u7684C\u4ee3\u7801\u6709\u4e00\u4e2a\u6b63\u786e\u7684\u5934\u6587\u4ef6\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* sample.h */\n\n#include <math.h>\n\nextern int gcd(int, int);\nextern int in_mandel(double x0, double y0, int n);\nextern int divide(int a, int b, int *remainder);\nextern double avg(double *a, int n);\n\ntypedef struct Point {\n    double x,y;\n} Point;\n\nextern double distance(Point *p1, Point *p2);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u8fd9\u4e2a\u5934\u6587\u4ef6\u8981\u5bf9\u5e94\u4e00\u4e2a\u5df2\u7ecf\u88ab\u5355\u72ec\u7f16\u8bd1\u8fc7\u7684\u5e93\u3002\n\u6709\u4e86\u8fd9\u4e9b\uff0c\u4e0b\u9762\u6211\u4eec\u6f14\u793a\u4e0b\u7f16\u5199\u6269\u5c55\u51fd\u6570\u7684\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "#include \"Python.h\"\n#include \"sample.h\"\n\n/* int gcd(int, int) */\nstatic PyObject *py_gcd(PyObject *self, PyObject *args) {\n  int x, y, result;\n\n  if (!PyArg_ParseTuple(args,\"ii\", &x, &y)) {\n    return NULL;\n  }\n  result = gcd(x,y);\n  return Py_BuildValue(\"i\", result);\n}\n\n/* int in_mandel(double, double, int) */\nstatic PyObject *py_in_mandel(PyObject *self, PyObject *args) {\n  double x0, y0;\n  int n;\n  int result;\n\n  if (!PyArg_ParseTuple(args, \"ddi\", &x0, &y0, &n)) {\n    return NULL;\n  }\n  result = in_mandel(x0,y0,n);\n  return Py_BuildValue(\"i\", result);\n}\n\n/* int divide(int, int, int *) */\nstatic PyObject *py_divide(PyObject *self, PyObject *args) {\n  int a, b, quotient, remainder;\n  if (!PyArg_ParseTuple(args, \"ii\", &a, &b)) {\n    return NULL;\n  }\n  quotient = divide(a,b, &remainder);\n  return Py_BuildValue(\"(ii)\", quotient, remainder);\n}\n\n/* Module method table */\nstatic PyMethodDef SampleMethods[] = {\n  {\"gcd\",  py_gcd, METH_VARARGS, \"Greatest common divisor\"},\n  {\"in_mandel\", py_in_mandel, METH_VARARGS, \"Mandelbrot test\"},\n  {\"divide\", py_divide, METH_VARARGS, \"Integer division\"},\n  { NULL, NULL, 0, NULL}\n};\n\n/* Module structure */\nstatic struct PyModuleDef samplemodule = {\n  PyModuleDef_HEAD_INIT,\n\n  \"sample\",           /* name of module */\n  \"A sample module\",  /* Doc string (may be NULL) */\n  -1,                 /* Size of per-interpreter state or -1 */\n  SampleMethods       /* Method table */\n};\n\n/* Module initialization function */\nPyMODINIT_FUNC\nPyInit_sample(void) {\n  return PyModule_Create(&samplemodule);\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u7ed1\u5b9a\u8fd9\u4e2a\u6269\u5c55\u6a21\u5757\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a setup.py \u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# setup.py\nfrom distutils.core import setup, Extension\n\nsetup(name='sample',\n      ext_modules=[\n        Extension('sample',\n                  ['pysample.c'],\n                  include_dirs = ['/some/dir'],\n                  define_macros = [('FOO','1')],\n                  undef_macros = ['BAR'],\n                  library_dirs = ['/usr/local/lib'],\n                  libraries = ['sample']\n                  )\n        ]\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u6784\u5efa\u6700\u7ec8\u7684\u51fd\u6570\u5e93\uff0c\u53ea\u9700\u7b80\u5355\u7684\u4f7f\u7528 python3 buildlib.py build_ext --inplace \u547d\u4ee4\u5373\u53ef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 setup.py build_ext --inplace\nrunning build_ext\nbuilding 'sample' extension\ngcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes\n -I/usr/local/include/python3.3m -c pysample.c\n -o build/temp.macosx-10.6-x86_64-3.3/pysample.o\ngcc -bundle -undefined dynamic_lookup\nbuild/temp.macosx-10.6-x86_64-3.3/pysample.o \\\n -L/usr/local/lib -lsample -o sample.so\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u4e0a\u6240\u793a\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u5b57\u53eb sample.so \u7684\u5171\u4eab\u5e93\u3002\u5f53\u88ab\u7f16\u8bd1\u540e\uff0c\u4f60\u5c31\u80fd\u5c06\u5b83\u4f5c\u4e3a\u4e00\u4e2a\u6a21\u5757\u5bfc\u5165\u8fdb\u6765\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sample\nsample.gcd(35, 42)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.in_mandel(0, 0, 500)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.in_mandel(2.0, 1.0, 500)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.divide(42, 8)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u662f\u5728Windows\u673a\u5668\u4e0a\u9762\u5c1d\u8bd5\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u73af\u5883\u548c\u7f16\u8bd1\u95ee\u9898\uff0c\u4f60\u9700\u8981\u82b1\u66f4\u591a\u70b9\u65f6\u95f4\u53bb\u914d\u7f6e\u3002\nPython\u7684\u4e8c\u8fdb\u5236\u5206\u53d1\u901a\u5e38\u4f7f\u7528\u4e86Microsoft  Visual Studio\u6765\u6784\u5efa\u3002\n\u4e3a\u4e86\u8ba9\u8fd9\u4e9b\u6269\u5c55\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u540c\u6837\u6216\u517c\u5bb9\u7684\u5de5\u5177\u6765\u7f16\u8bd1\u5b83\u3002\n\u53c2\u8003\u76f8\u5e94\u7684 Python\u6587\u6863"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5c1d\u8bd5\u4efb\u4f55\u624b\u5199\u6269\u5c55\u4e4b\u524d\uff0c\u6700\u597d\u80fd\u5148\u53c2\u8003\u4e0bPython\u6587\u6863\u4e2d\u7684\n\u6269\u5c55\u548c\u5d4c\u5165Python\u89e3\u91ca\u5668 .\nPython\u7684C\u6269\u5c55API\u5f88\u5927\uff0c\u5728\u8fd9\u91cc\u6574\u4e2a\u53bb\u8bb2\u8ff0\u5b83\u6ca1\u4ec0\u4e48\u5b9e\u9645\u610f\u4e49\u3002\n\u4e0d\u8fc7\u5bf9\u4e8e\u6700\u6838\u5fc3\u7684\u90e8\u5206\u8fd8\u662f\u53ef\u4ee5\u8ba8\u8bba\u4e0b\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\uff0c\u5728\u6269\u5c55\u6a21\u5757\u4e2d\uff0c\u4f60\u5199\u7684\u51fd\u6570\u90fd\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\u4e00\u4e2a\u666e\u901a\u539f\u578b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_func(PyObject *self, PyObject *args) {\n  ...\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "PyObject \u662f\u4e00\u4e2a\u80fd\u8868\u793a\u4efb\u4f55Python\u5bf9\u8c61\u7684C\u6570\u636e\u7c7b\u578b\u3002\n\u5728\u4e00\u4e2a\u9ad8\u7ea7\u5c42\u9762\uff0c\u4e00\u4e2a\u6269\u5c55\u51fd\u6570\u5c31\u662f\u4e00\u4e2a\u63a5\u53d7\u4e00\u4e2aPython\u5bf9\u8c61\n\uff08\u5728 PyObject *args\u4e2d\uff09\u5143\u7ec4\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0Python\u5bf9\u8c61\u7684C\u51fd\u6570\u3002\n\u51fd\u6570\u7684 self \u53c2\u6570\u5bf9\u4e8e\u7b80\u5355\u7684\u6269\u5c55\u51fd\u6570\u6ca1\u6709\u88ab\u4f7f\u7528\u5230\uff0c\n\u4e0d\u8fc7\u5982\u679c\u4f60\u60f3\u5b9a\u4e49\u65b0\u7684\u7c7b\u6216\u8005\u662fC\u4e2d\u7684\u5bf9\u8c61\u7c7b\u578b\u7684\u8bdd\u5c31\u80fd\u6d3e\u4e0a\u7528\u573a\u4e86\u3002\u6bd4\u5982\u5982\u679c\u6269\u5c55\u51fd\u6570\u662f\u4e00\u4e2a\u7c7b\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\n\u90a3\u4e48 self \u5c31\u80fd\u5f15\u7528\u90a3\u4e2a\u5b9e\u4f8b\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "PyArg_ParseTuple() \u51fd\u6570\u88ab\u7528\u6765\u5c06Python\u4e2d\u7684\u503c\u8f6c\u6362\u6210C\u4e2d\u5bf9\u5e94\u8868\u793a\u3002\n\u5b83\u63a5\u53d7\u4e00\u4e2a\u6307\u5b9a\u8f93\u5165\u683c\u5f0f\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8f93\u5165\uff0c\u6bd4\u5982\u201ci\u201d\u4ee3\u8868\u6574\u6570\uff0c\u201cd\u201d\u4ee3\u8868\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\uff0c\n\u540c\u6837\u8fd8\u6709\u5b58\u653e\u8f6c\u6362\u540e\u7ed3\u679c\u7684C\u53d8\u91cf\u7684\u5730\u5740\u3002\n\u5982\u679c\u8f93\u5165\u7684\u503c\u4e0d\u5339\u914d\u8fd9\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5c31\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u5e76\u8fd4\u56de\u4e00\u4e2aNULL\u503c\u3002\n\u901a\u8fc7\u68c0\u67e5\u5e76\u8fd4\u56deNULL\uff0c\u4e00\u4e2a\u5408\u9002\u7684\u5f02\u5e38\u4f1a\u5728\u8c03\u7528\u4ee3\u7801\u4e2d\u88ab\u629b\u51fa\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Py_BuildValue() \u51fd\u6570\u88ab\u7528\u6765\u6839\u636eC\u6570\u636e\u7c7b\u578b\u521b\u5efaPython\u5bf9\u8c61\u3002\n\u5b83\u540c\u6837\u63a5\u53d7\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u6307\u5b9a\u671f\u671b\u7c7b\u578b\u3002\n\u5728\u6269\u5c55\u51fd\u6570\u4e2d\uff0c\u5b83\u88ab\u7528\u6765\u8fd4\u56de\u7ed3\u679c\u7ed9Python\u3002\nPy_BuildValue() \u7684\u4e00\u4e2a\u7279\u6027\u662f\u5b83\u80fd\u6784\u5efa\u66f4\u52a0\u590d\u6742\u7684\u5bf9\u8c61\u7c7b\u578b\uff0c\u6bd4\u5982\u5143\u7ec4\u548c\u5b57\u5178\u3002\n\u5728 py_divide() \u4ee3\u7801\u4e2d\uff0c\u4e00\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e86\u600e\u6837\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u3002\u4e0d\u8fc7\uff0c\u4e0b\u9762\u8fd8\u6709\u4e00\u4e9b\u5b9e\u4f8b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "return Py_BuildValue(\"i\", 34);      // Return an integer\nreturn Py_BuildValue(\"d\", 3.4);     // Return a double\nreturn Py_BuildValue(\"s\", \"Hello\"); // Null-terminated UTF-8 string\nreturn Py_BuildValue(\"(ii)\", 3, 4); // Tuple (3, 4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u6269\u5c55\u6a21\u5757\u5e95\u90e8\uff0c\u4f60\u4f1a\u53d1\u73b0\u4e00\u4e2a\u51fd\u6570\u8868\uff0c\u6bd4\u5982\u672c\u8282\u4e2d\u7684 SampleMethods \u8868\u3002\n\u8fd9\u4e2a\u8868\u53ef\u4ee5\u5217\u51faC\u51fd\u6570\u3001Python\u4e2d\u4f7f\u7528\u7684\u540d\u5b57\u3001\u6587\u6863\u5b57\u7b26\u4e32\u3002\n\u6240\u6709\u6a21\u5757\u90fd\u9700\u8981\u6307\u5b9a\u8fd9\u4e2a\u8868\uff0c\u56e0\u4e3a\u5b83\u5728\u6a21\u5757\u521d\u59cb\u5316\u65f6\u8981\u88ab\u4f7f\u7528\u5230\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u7684\u51fd\u6570 PyInit_sample() \u662f\u6a21\u5757\u521d\u59cb\u5316\u51fd\u6570\uff0c\u4f46\u8be5\u6a21\u5757\u7b2c\u4e00\u6b21\u88ab\u5bfc\u5165\u65f6\u6267\u884c\u3002\n\u8fd9\u4e2a\u51fd\u6570\u7684\u4e3b\u8981\u5de5\u4f5c\u662f\u5728\u89e3\u91ca\u5668\u4e2d\u6ce8\u518c\u6a21\u5757\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u4e2a\u8981\u70b9\u9700\u8981\u63d0\u51fa\u6765\uff0c\u4f7f\u7528C\u51fd\u6570\u6765\u6269\u5c55Python\u8981\u8003\u8651\u7684\u4e8b\u60c5\u8fd8\u6709\u5f88\u591a\uff0c\u672c\u8282\u53ea\u662f\u4e00\u5c0f\u90e8\u5206\u3002\n\uff08\u5b9e\u9645\u4e0a\uff0cC API\u5305\u542b\u4e86\u8d85\u8fc7500\u4e2a\u51fd\u6570\uff09\u3002\u4f60\u5e94\u8be5\u5c06\u672c\u8282\u5f53\u505a\u662f\u4e00\u4e2a\u5165\u95e8\u7bc7\u3002\n\u66f4\u591a\u9ad8\u7ea7\u5185\u5bb9\uff0c\u53ef\u4ee5\u770b\u770b PyArg_ParseTuple() \u548c Py_BuildValue() \u51fd\u6570\u7684\u6587\u6863\uff0c\n\u7136\u540e\u8fdb\u4e00\u6b65\u6269\u5c55\u5f00\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}